package com.nuanshui.heatedloan.controller.display.common;

import com.alibaba.dubbo.config.annotation.Reference;
import com.nuanshui.heatedloan.constant.GlobalParam;
import com.nuanshui.heatedloan.controller.BaseController;
import com.nuanshui.heatedloan.msgcenter.facade.*;
import com.nuanshui.heatedloan.msgcenter.model.MsgBizType;
import com.nuanshui.heatedloan.msgcenter.vo.SystemMsgVO;
import com.nuanshui.heatedloan.pageable.Pager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequestMapping("/msg")
@RestController
public class MsgController extends BaseController {

    private Logger log = LoggerFactory.getLogger(getClass());

    @Reference(url = GlobalParam.MSGCENTER_DUBBO_SERVER, timeout = GlobalParam.DUBBO_SERVER_TIME_OUT,version = GlobalParam.MSGCENTER_DUBBO_SERVER_VERSION )
    private SystemMsgService systemMsgService;

    @Reference(url = GlobalParam.MSGCENTER_DUBBO_SERVER, timeout = GlobalParam.DUBBO_SERVER_TIME_OUT,version = GlobalParam.MSGCENTER_DUBBO_SERVER_VERSION )
    private MessageService messageService;

    @Reference(url = GlobalParam.MSGCENTER_DUBBO_SERVER, timeout = GlobalParam.DUBBO_SERVER_TIME_OUT,version = GlobalParam.MSGCENTER_DUBBO_SERVER_VERSION )
    private MsgRedDotStatusService msgRedDotStatusService;

    private List<SystemMsgVO> loadMsgList(Integer type,Pager<SystemMsgVO> pager) {

        String userId = getCurrentUser().getId();

        MsgBizType bizType = MsgBizType.getBizType(type);
        pager = systemMsgService.findAllSystemMsg(bizType.getType(),bizType==MsgBizType.SYSTEM ? null : userId,pager);
        List<SystemMsgVO> msgList = pager.getData();

        messageService.resetUserStatus(userId, bizType);

        return msgList;
    }

    /**
     * 获取系统消息
     *
     * @return
     */
    @RequestMapping(value = "/loadSysMsg",method = RequestMethod.POST)
    public String loadSysMsg(Pager<SystemMsgVO> pager) {
        return success(loadMsgList(MsgBizType.SYSTEM.getType(),pager));
    }

    /**
     * 获取还款消息列表
     *
     * @Author: Gonzo
     * @Date 2017/4/5下午6:47
     * @since 1.4.2
     */
    @RequestMapping(value = "/loadPaymentMsg",method = RequestMethod.POST)
    public String loadPaymentMsg(Pager<SystemMsgVO> pager) {
        return success(loadMsgList(MsgBizType.REPAYMENT.getType(),pager));
    }

    /**
     * 获取审核消息列表
     *
     * @Author: Gonzo
     * @Date 2017/4/5下午6:47
     * @since 1.4.2
     */
    @RequestMapping(value = "/loadAuditMsg",method = RequestMethod.POST)
    public String loadAuditMsg(Pager<SystemMsgVO> pager) {
        return success(loadMsgList(MsgBizType.AUDIT.getType(),pager));
    }

    /**
     * 获取消息详情
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/loadMsgDetail",method = RequestMethod.POST)
    public String loadSystemMsgDetail(String id) {
        SystemMsgVO systemMsgVO = systemMsgService.loadSystemMsgById(id);
        return success(systemMsgVO);
    }

    /**
     * 获取信息红点的状态
     *
     * @param @return
     * @author guohao
     * @date 2017年1月17日 下午2:43:31
     * @since $
     */
    @RequestMapping(value = "/newMsg/status",method = RequestMethod.POST)
    public String msgStatus() {
        String userId = getCurrentUser().getId();
        return success(msgRedDotStatusService.getMsgStatus(userId));
    }

}
